/* GB2312 Encoding */
/*-----------------------------------------------------------------------
|                            FILE DESCRIPTION                           |
-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
  - File name     : stm32_uart.h
  - Author        : liuzhihua (liuzhihuawy@163.com)
  - Update date   : 2024.03.09
  -	Brief         : STM32 uart FIFO
  - Version       : v1.0
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               UPDATE NOTE                             |
-----------------------------------------------------------------------*/
/**
  * Update note:
  * ------------   ---------------   ----------------------------------
  *     Date            Author                      Note
  * ------------   ---------------   ----------------------------------
  *  2023.04.25       liuzhihua                  Create file          
***/

#ifndef _STM32_UART_H_
#define _STM32_UART_H_

#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                                NCLUDES                                |
-----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>

#include "cQueue.h"

/*-----------------------------------------------------------------------
|                                DEFINES                                |
-----------------------------------------------------------------------*/
//<o> Format Buffer Size <i> 格式化缓存
#define FORMAT_BUFFER_SIZE 128
//<o> Receive Buffer Size <i> 一帧数据的最大长度
#define RXMSG_BUFFER_SIZE 64

typedef struct UARTRX_MGS_Structure
{
    uint16_t size;
    uint8_t data[RXMSG_BUFFER_SIZE];
} RX_MSG_t;

typedef struct UART_RXFIFO_Structure
{
    UART_HandleTypeDef *huart;
    uint8_t rxCpltFlag;
    uint8_t write:1;
    RX_MSG_t Msg[2];    //双缓存
} RxHandle_t;

typedef struct UART_TXFIFO_Structure
{
    UART_HandleTypeDef *huart;
    cQueue_t cQ;
    uint16_t sendsize;
} TxHandle_t;


/*-----------------------------------------------------------------------
|                                  API                                  |
-----------------------------------------------------------------------*/
#define UART_TAG "UART"
#define Log(x, ...) //LOG_DBG(UART_TAG, (const char*)(x), ##__VA_ARGS__)
#define Logs(s) //LOG_INFO(UART_TAG, (const char*)(s))

void User_USARTx_Init(RxHandle_t *rxFIFO, uint8_t uart_id);
void User_UART_Receive_Start(RxHandle_t *msgbuf);
void UART_RxHandle_CreateStatic(UART_HandleTypeDef *huart, RxHandle_t *hrx);
void UART_TxHandle_CreateStatic(UART_HandleTypeDef *huart, TxHandle_t *htx, 
							                  uint8_t *dataBuf, uint16_t bufSize);
uint8_t *UART_GetRxData(RxHandle_t *hrx);
uint16_t UART_GetRxSize(RxHandle_t *hrx);
void TxHandle_TxCpltCallback(TxHandle_t *htx);
void RxHandle_RxEventCallback(RxHandle_t *hrx, uint16_t size);
int UARTx_Printf(UART_HandleTypeDef* huart,const char* fmt,...);
int UARTx_Print(UART_HandleTypeDef *huart, const char *str);
int UARTx_Printn(UART_HandleTypeDef *huart, uint8_t *pdata, int len);
int Queue_Printn(TxHandle_t *htx, uint8_t *pdata, size_t cnt);
int Queue_Printf(TxHandle_t *htx, const char *fmt, ...);
int Queue_Print(TxHandle_t *htx, const char *str);

#ifdef __cplusplus
	}
#endif
#endif
